package com.android.mangalib.adapter;

import java.text.SimpleDateFormat;
import java.util.List;

import com.android.mangalib.activity.R;
import com.android.mangalib.object.Tome;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class TomeAdapter extends BaseAdapter {
	
	List<Tome> collection;
	LayoutInflater inflater;

	public TomeAdapter(Context context, List<Tome> collection) {
		inflater = LayoutInflater.from(context);
		this.collection = collection;
	}
	
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return collection.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return collection.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		TomeViewHolder holder;
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		
		if(convertView == null) {
			holder = new TomeViewHolder();
			
			// Get the layout for printing the list of tome	
			convertView = inflater.inflate(R.layout.itemtome, null);
			
			// Get the xml object for the view
			holder.tvTitle = (TextView)convertView.findViewById(R.id.tvTitle);
			holder.tvDate = (TextView)convertView.findViewById(R.id.tvDate);
			convertView.setTag(holder);
		}
		else {
			holder = (TomeViewHolder)convertView.getTag();
		}
		
		// Set the title and date to print on screen
		holder.tvTitle.setText(collection.get(position).getTitle());
		holder.tvDate.setText(sdf.format(collection.get(position).getPublication_date()));
		
		// Set a red background color the item in the list if not owned
		if(!collection.get(position).isOwned())
			convertView.setBackgroundColor(Color.RED);
		
		return convertView;
	}
	
	public class TomeViewHolder {
		
		public TextView tvTitle, tvDate;

	}

}
